package jgl.vajra.operator.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.databinding.BindingAdapter;
import android.util.AttributeSet;
import android.view.View;

import jgl.vajra.operator.R;
import jgl.vajra.operator.widget.base.BaseCustomView;
import jgl.vajra.operator.databinding.ViewArrowExpandableBinding;
import jgl.vajra.operator.widget.interfaces.ArrowExpandableViewListener;

/**
 * Created by Administrator on 2018/8/20.
 */

public class ArrowExpandableView extends BaseCustomView<ViewArrowExpandableBinding> {
    private View relationView;
    private ArrowExpandableViewListener mListener;

    public ArrowExpandableView(Context context) {
        super(context);
    }

    public ArrowExpandableView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void initEvent(Context context) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_arrow_expandable;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArrowExpandableView);
        int arrowImage = typedArray.getResourceId(R.styleable.ArrowExpandableView_arrowImage, R.drawable.ic_black_arrow_down);
        mBinding.ivArrow.setImageResource(arrowImage);
        typedArray.recycle();
        mBinding.setView(this);
        mBinding.ivArrow.setRotation(180);
    }

    public void setExpandableView(boolean expandableView) {
        isExpandableView = expandableView;
        if (isExpandableView) {
            mBinding.ivArrow.setRotation(180);
        } else {
            mBinding.ivArrow.setRotation(0);
        }
    }

    public void setArrowExpandableViewListener(ArrowExpandableViewListener listener) {
        this.mListener = listener;
    }

    //展开
    private boolean isExpandableView = true;

    public void setRelationView(View view) {
        this.relationView = view;
    }

    public void expandableView() {
        setExpandableView();
        if (mListener != null)
            mListener.onViewClick(isExpandableView);
    }

    private void setExpandableView() {
        if (!isExpandableView) {
            if (relationView != null)
                relationView.setVisibility(VISIBLE);
            mBinding.ivArrow.setRotation(180);
        } else {
            mBinding.ivArrow.setRotation(0);
            if (relationView != null)
                relationView.setVisibility(GONE);
        }
        isExpandableView = !isExpandableView;
    }

    @BindingAdapter("visibilityArrow")
    public static void setVisibilityArrow(ArrowExpandableView v, boolean isVisibilityArrow) {
        v.mBinding.ivArrow.setVisibility(isVisibilityArrow ? VISIBLE : GONE);
    }

}
